static void do_logprintf(const char *file, unsigned line, const char* format, ...);
int main(int argc, char **argv);
static void input();
static void init_renderer();
static void fini_renderer();
static void render();
static void loop_core(int *credit);
static void init_game();
static void fini_game();
static void init_threads(YieldReason entry_point);
static void run_main_ctx(YieldReason entry);
static void run_int_ctx();
static unsigned checksum(Mem *m);
static void rom_load(void *mem, const char* name, size_t offset, size_t len);
static void load_rom(void *mem);
static int execute(int allowed);
static void switch_to(ucontext_t *to);
static void co_switch(ucontext_t *prev, ucontext_t *next);
static void timeslice();
static void yield(YieldReason reason);
static uint8_t get_input(int64_t ticks, uint8_t port);
static uint8_t read_port(uint8_t port);
static void write_port(uint16_t port, uint8_t v);
static void enable_interrupts();
static void irq(uint8_t v);
static int interrupted();
static void fatalerror(const char* format, ...);
static inline Word u16_to_word(uint16_t u);
static inline Word u8_u8_to_word(uint8_t h, uint8_t l);
static inline uint16_t ptr_to_u16(uint8_t *ptr);
static inline Word ptr_to_word(uint8_t *ptr);
static inline uint8_t* u16_to_ptr(uint16_t u);
static inline uint8_t* word_to_ptr(Word w);
static int is_godmode();
static uint8_t* rompos(uint8_t* ram);
static uint8_t bcd_add(uint8_t bcd, uint8_t a, uint8_t *carry);
static void do_logprintf(const char *file, unsigned line, const char* format, ...);
static void DebugMessage(Word sc, uint8_t* msg, uint8_t n);
static void reset();
static void midscreen_int();
static void vblank_int();
static void vblank_coins();
static void InitRack();
static void InitAlienRacks();
static void DrawAlien();
static void CursorNextAlien();
static Word GetAlienCoords(uint8_t *rowout, uint8_t k);
static void InitAliensSub(uint8_t* dest);
static void InitAliensP1();
static void DrawBottomLine();
static uint8_t AddDelta(uint8_t* vecptr, uint8_t c);
static void RestoreFromROM(uint8_t* addr, size_t n);
static void CopyRomtoRam();
static void CopyRAMMirror();
static void DrawShieldP1();
static void DrawShieldP2();
static void DrawShieldCommon(uint8_t* dest);
static void RememberShieldsP1();
static void RememberShieldsP2();
static void RestoreShieldsP2();
static void RestoreShieldsP1();
static void CopyShields(uint8_t dir, uint8_t* sprbuf);
static void RunGameObjs(uint8_t* ptr);
static void GameObj0(uint8_t* unused);
static void HandleBlowingUpPlayer(uint8_t anim);
static void player_death(int invaded);
static int player_death_sub(int invaded);
static void DrawPlayerShot(int op);
static void GameObj1(uint8_t* unused);
static SprDesc ReadPlyShot();
static void GameObj2(uint8_t* unused1);
static void GameObj3(uint8_t* unused);
static void ProcessSquigglyShot();
static void ToShotStruct(AShot* src, uint8_t picend);
static void FromShotStruct(AShot* dest);
static void HandleAlienShot();
static int DoHandleAlienShotMove();
static void HandleAlienShotMove();
static uint8_t FindInColumn(uint8_t *out, uint8_t col);
static void ShotBlowingUp();
static void DrawAlienShot();
static void EraseAlienShot();
static void GameObj4(uint8_t* unused);
static void RemoveSaucer();
static SprDesc GetSaucerInfo();
static void DrawPlayer();
static void DrawSaucer();
static void WaitForStart();
static void NewGame(uint8_t is2p, uint8_t cost, int skip);
static uint8_t* GetRefAlienInfo(uint8_t *dxr, Word *pos);
static uint8_t* GetAlRefPtr();
static void PromptPlayer();
static uint8_t GetShipsPerCred();
static void SpeedShots();
static void PrintMessage(Word sc, uint8_t* msg, size_t n);
static Word DrawChar(Word sc, uint8_t c);
static void TimeToSaucer();
static uint8_t GetNumberOfShips(uint8_t* *ptr);
static void CheckAndHandleExtraShipAward();
static uint8_t* AlienScoreValue(uint8_t row);
static void AdjustScore();
static void Print4Digits(Word sc, Word val);
static Word DrawHexByte(Word sc, uint8_t c);
static Word DrawHexByteSub(Word sc, uint8_t c);
static uint8_t* GetScoreDescriptor();
static void ClearPlayField();
static void HandleEndOfTurn();
static void HandleEndOfTurnSub();
static uint8_t IsPlayerAlive();
static void ScoreForAlien(uint8_t row);
static void Animate();
static Word PrintMessageDel(Word sc, uint8_t* str, uint8_t n);
static void SplashSquiggly();
static void OneSecDelay();
static void TwoSecDelay();
static void SplashDemo();
static void ISRSplTasks();
static void MessageToCenterOfScreen(uint8_t* str);
static void WaitOnDelay(uint8_t n);
static void IniSplashAni(uint8_t* src);
static uint8_t CheckPlyrShotAndBump();
static void EraseSimpleSprite(Word pos, uint8_t n);
static Word DrawSimpSprite(SprDesc *desc);
static Word CnvtPixNumber(Word pos);
static void DrawShiftedSprite(struct SprDesc *desc);
static void EraseShifted(struct SprDesc *desc);
static void DrawSprCollision(struct SprDesc *desc);
static void DrawSprite(struct SprDesc *desc);
static void DrawSpriteGeneric(struct SprDesc *desc, int op);
static Word ClearSmallSprite(Word sc, uint8_t n, uint8_t v);
static void PlayerShotHit();
static uint8_t Cnt16s(uint8_t *v, uint8_t tgt);
static Word FindRow(uint8_t y);
static Word FindColumn(uint8_t x);
static uint8_t* GetAlienStatPtr(uint8_t row, uint8_t col);
static void RackBump();
static uint8_t RackBumpEdge(Word sc);
static void CountAliens();
static uint8_t* GetPlayerDataPtr();
static void PlrFireOrDemo();
static void GameOver();
static void on_invaded();
static void AShotReloadRate();
static void ShotSound();
static void TimeFleetSound();
static void DisableFleetSound();
static void SetSoundWithoutFleet(uint8_t v);
static uint8_t FleetDelayExShip();
static uint8_t ReadInputs();
static void CheckHandleTilt();
static void on_tilt();
static void CtrlSaucerSound();
static void DrawAdvTable();
static void Draw16ByteSprite(Word sc, uint8_t* sprite);
static void PrintMessageAdv(Word sc, uint8_t* msg);
static int ReadPriStruct(PriCursor *pri);
static void SplashSprite();
static void AnimateShootingSplashAlien();
static void main_init(int skip);
static uint8_t* GetOtherPlayerAliveFlag();
static void SoundBits3On(uint8_t mask);
static void InitAliensP2();
static void PlyrShotAndBump();
static uint8_t* CurPlyAlive();
static void DrawScoreHead();
static void PrintP1Score();
static void PrintP2Score();
static void DrawScore(uint8_t* iter);
static void PrintCreditLabel();
static void DrawNumCredits();
static void PrintHiScore();
static void DrawStatus();
static void PrintTaitoCorporation();
static void CheckHiddenMes();
static void EnableGameTasks();
static void DsableGameTasks();
static void SoundBits3Off(uint8_t mask);
static void DrawNumShips(uint8_t n);
static void DrawNumShipsSub(Word pos);
static uint8_t CompXrToBeam(uint8_t* posaddr);
static void BlockCopy(void *_dest, void *_src, size_t n);
static SprDesc ReadDesc(SprDesc* src);
static Word ConvToScr(Word pos);
static void ClearScreen();
static void CopyShieldBuffer(ShieldBufferCursor *cursor, Word spr_size, uint8_t dir);
static void RemoveShip();
static void PrintNumShips(uint8_t num);
